package com.school.dao.impl;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.school.dao.ClassRoomDao;
import com.school.domain.ClassRoom;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class ClassRoomDaoImpl implements ClassRoomDao {
    private DataSource dataSource = new ComboPooledDataSource();
    private QueryRunner queryRunner = new QueryRunner(dataSource);

    @Override
    public boolean add(ClassRoom classRoom) {
        int result = 0;
        try {
            result = queryRunner.update("insert into classRoom values(?, ?, ?, ?)", null, classRoom.getName(), classRoom.getNum(), 0);

        } catch (SQLException throwables) {
            return false;
        }
        if (result == 1) return true;
        return false;
    }

    @Override
    public boolean del(long rid) {
        int result = 0;
        try {
            result = queryRunner.update("delete from classRoom where rid = ?", rid);
        } catch (SQLException throwables) {
            return false;
        }
        if (result == 1) return true;
        return false;
    }

    @Override
    public boolean del(String name) {
        int result = 0;
        try {
            result = queryRunner.update("delete from classRoom where name = ?", name);
        } catch (SQLException throwables) {
            return false;
        }
        if (result == 1) return true;
        return false;
    }

    @Override
    public boolean update(ClassRoom classRoom) {
        int result = 0;
        try {
            result = queryRunner.update("update classRoom set name = ?, num = ?, nowNum = ? where rid = ?", classRoom.getName(), classRoom.getNum(), classRoom.getNowNum(), classRoom.getRid());
        } catch (SQLException throwables) {
            return false;
        }
        if (result == 1) return true;
        return false;
    }

    @Override
    public ClassRoom findById(long rid) {
        List<ClassRoom> execute = null;
        try {
            execute = queryRunner.query("select * from classRoom where rid = " + rid, new BeanListHandler<ClassRoom>(ClassRoom.class));
        } catch (SQLException throwables) {
            return null;
        }
        if (execute.size() < 1) return null;
        return execute.get(0);
    }

    @Override
    public ClassRoom findByName(String name) {
        List<ClassRoom> execute = null;
        try {
            execute = queryRunner.query("select * from classRoom where name = " + name, new BeanListHandler<ClassRoom>(ClassRoom.class));
        } catch (SQLException throwables) {
            return null;
        }
        if (execute.size() < 1) return null;
        return execute.get(0);
    }

    @Override
    public List<ClassRoom> findAll() {
        List<ClassRoom> execute = null;
        try {
            execute = queryRunner.query("select * from classRoom", new BeanListHandler<ClassRoom>(ClassRoom.class));
        } catch (SQLException throwables) {
            return null;
        }
        return execute;
    }
}
